// pages/tabbar/index/product/index.js
const App = getApp();
const {Dialog, Toast} = require("../../../../config/modal")
const utils = require("../../../../utils/util")
const {LOGIN, LOCATION} = require("../../../../config/login.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        location_icon: '/static/img/school_location.png',
        title: '校园杂货铺',
        capsuleBarHeight: App.$getMenuButtonHeight(),
        offsetTop: App.globalData.window.navHeight,
        tabColor: "black",
        tabActive: 0,
        showSearch: true,
        // 用户和地址信息
        userInfo: {},
        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },
        product: {
            typeList: [],
            list: [],
            page: {
                pageSize: 20,
                pageNum: 1,
                pages: 1,
            }
        },
        searchParams: {pageSize: 20},
        
    },
    URL: {
        pageList: '/product/pageList',
        typeList: '/product/productTypes',
    },
    page: {
        home: '/pages/tabbar/index/index',
        schoolPage: "/pages/school/index",
        detail:"/pages/tabbar/index/product/detail/index"
    },

    initSchoolName() {
        let schoolName = App.$getData('product_schoolName');
        let userInfo = App.$getUserInfo();
        if (utils.isInvalid(schoolName)) {
            if (!utils.isInvalid(userInfo.schoolName)) {
                schoolName = userInfo.schoolName;
                this.setData({
                  userInfo:userInfo
                })
            }
        } else {
            this.setData({
                'userInfo.schoolName': schoolName
            })
        }
        if (!utils.isInvalid(schoolName)) {
            App.$saveData('product_schoolName', schoolName);
        }
        return schoolName;
    },
    clearSchoolName() {
        this.setData({
            'searchParams.schoolName': '',
            'userInfo.schoolName': null,
        })
        App.$removeStore('product_schoolName');
        this.initData(this.data.searchParams, false);
    },
    // 选择学校后回调函数
    onSchoolNameChange(e) {
        this.setData({
            'searchParams.pageNum': 1,
            'searchParams.schoolName': e.detail,
            'userInfo.schoolName': e.detail,
        })
        App.$saveData('product_schoolName', e.detail);
        this.initData(this.data.searchParams, false);
    },
    // 点击选择学校
    reloadSchoolName() {
        App.$navigateTo(this.page.schoolPage, false)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            'searchParams.schoolName': this.initSchoolName(),
        })
        // 检查是否被邀请进来的
        App.$handlerInviteCode(options);

        this.getTypeList();
        setTimeout(res=>{
            this.initData(this.data.searchParams, false);
        },200)
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    closeSearch() {
        this.setData({
            showSearch: false
        })
    },
    onSearch(e) {
        this.setData({
            'searchParams.description': e.detail,
            'searchParams.pageNum': 1
        })
        this.initData(this.data.searchParams, false);
    },
    setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })

    },
    onClearSearch() {
        this.setData({
            'searchParams.description': "",
            'searchParams.pageNum': 1
        })
        this.initData(this.data.searchParams, false);
    },
    tabOnChange(e) {
        let value = (e.detail.name == 0||e.detail.name == '') ? '' : e.detail.name;
        this.setData({
            'searchParams.typeNos': value
        })
        this.data.searchParams.pageNum = 1;
        this.initData(this.data.searchParams, false);
    },
    getTypeList() {
        App.$get(true, this.URL.typeList, {}, res => {
            if (res.R) {
                this.setData({
                    'product.typeList': res.data
                })
            }
        })
    }
    , initData(params, bottom) {
        this.setLoadingMore(bottom, "loading", "加载中...")
        App.$get(true, this.URL.pageList, params, res => {
            wx.stopPullDownRefresh();
            this.setLoadingMore(false, "", "")
            if (res.R) {
                this.setPages(res);
                if (!bottom) {
                    this.setData({
                        'product.list': res.data.rows
                    })
                    return;
                }
                let source = res.data.product.list;
                res.data.rows.forEach(e => {
                    source.push(e);
                })
                this.setData({
                    'product.list': source
                })
            }
        })
    },
    setPages(res) {
        this.setData({
            'product.page.pageNum': res.data.pageNum,
            'product.page.pageSize': res.data.pageSize,
            'product.page.pages': res.data.pages,
        })
    },

    goBack() {
        wx.navigateBack();
    },
    goHome() {
        App.$switchTab(this.page.home, false)
    },


    makePhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.userphone
        })
    },
    copyWechat(e) {
        let wechat = e.currentTarget.dataset.wechat;
        wx.setClipboardData({
            data: wechat,
            success: function (res) {
                wx.getClipboardData({
                    success: function () {
                        wx.showToast({
                            title: '已复制',
                            icon:'none'
                        })
                    }
                })
            }
        })
    },

    toDetail(e){
        let item = utils.getEvent(e).item;
        App.$navigateTo(this.page.detail+"?productNo="+item.productNo,false)
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.initData(this.data.searchParams);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let pageNum = this.data.product.page.pageNum + 1
        if (pageNum > this.data.product.page.pages) {
            this.setLoadingMore(true, "end", "我是有底线的~")
            Toast("没有更多数据啦～")
            return;
        }
        let params = this.data.searchParams;
        params.pageNum = pageNum;
        params.pageSize = 20;
        this.initData(params, true);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return App.$onShare('/pages/tabbar/index/product/index',null,"校园二手转让")
    }
})
